package com.cbwl.eoas.cms.web.controller.eoas;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cbwl.eoas.cms.api.eoas.IAppVersionService;
import com.cbwl.eoas.cms.api.sysconfig.IDictDetailService;
import com.cbwl.eoas.cms.model.eoas.AppVersionDTO;
import com.cbwl.eoas.cms.model.sysconfig.DictDetailDTO;
import com.cbwl.eoas.common.framework.exception.CustomException;
import com.cbwl.eoas.common.framework.session.SessionManager;
import com.cbwl.eoas.common.framework.vo.PageRequest;
import com.cbwl.eoas.common.util.constain.DicTypeConstant;
import com.cbwl.eoas.common.util.oss.OSSClientUtil;
import com.cbwl.eoas.common.web.Result;
import com.github.pagehelper.PageInfo;



/**
 * 
 * <p>[描述信息：app版本管理Controller]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月12日 下午13:29:23
 */
@Controller
@RequestMapping("/appversion")
public class AppVersionController {
	
	
	//业务层注解
	@Autowired
	private IDictDetailService dictDetailService;
	
	
	//业务层注解
	@Autowired
	private IAppVersionService appVersionService;
	
	
	@Autowired
	private OSSClientUtil oSSClientUtil;
	
	
	private static Logger logger = LoggerFactory.getLogger(AppVersionController.class);
	
	/**
	 * <p>初始化界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initAppVersion")
	public String initAppVersionPage(HttpServletRequest request) {
		return "initAppVersion";
	}


	/**
	 * <p>初始化新增修改界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initAddUpdateAppVersionPage")
	public String initAddUpdateAppVersionPage(HttpServletRequest request,AppVersionDTO bean) {
		request.setAttribute("bean", bean);
		
		DictDetailDTO detailBean = new DictDetailDTO();
		detailBean.setDictTypeNo(DicTypeConstant.state);
		request.setAttribute("detailBean", dictDetailService.queryObjectList(detailBean));
		return "initAddUpdateAppVersionPage";
	}
	

	/**
	 * <p>查询（分页）</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/queryAppVersionPage")
	@ResponseBody
	public Object queryAppVersionPage(HttpServletRequest request,AppVersionDTO bean,PageRequest pageRequest) {
		Result result = new Result(Result.Status.OK, "查询成功", "200");
		
		// 分页查询用户信息
		PageInfo<?> orgPageInfo =  appVersionService.queryPageList(bean,pageRequest);
		result.setData(orgPageInfo);
		return result;
	}

	/**
	 * 
	 * <p>通过id查询主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/getAppVersionById")
	@ResponseBody
	public Object getAppVersionById(HttpServletRequest request,AppVersionDTO bean) {
		return appVersionService.getObjectById(bean);
	}
	
	
	/**
	 * 
	 * <p>保存上传的apk文件</p>
	 * 
	 * @param request
	 * @param file
	 * @return
	 * @throws CustomException
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年12月4日 下午1:49:44
	 */
	@RequestMapping("/saveApkToOss")
	@ResponseBody
	public Object saveApkToOss(HttpServletRequest request,MultipartFile natrualImgFile) throws CustomException {
		
		String name = natrualImgFile.getName();
		
		if(StringUtils.isEmpty(name)){
			new CustomException("","文件不能为空");
		}
		
		logger.info("用户id={}上传了apk文件{}",SessionManager.getCurrentUser().getId(),name);
		return oSSClientUtil.uploadImg2Oss(natrualImgFile);
	}
	
	

	/**
	 * 
	 * <p>保存主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/saveAppVersion")
	@ResponseBody
	public Object saveAppVersion(HttpServletRequest request,AppVersionDTO bean) throws Exception {
		bean.setGmtCreate(new Date());
		bean.setCreater(SessionManager.getCurrentUser().getId());
		logger.info("{}新建了app版本{}",SessionManager.getCurrentUser().getEmpName(),bean.getVersionNo());
		return appVersionService.saveObject(bean);
	}

	/**
	 * 
	 * <p>更新主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/updateAppVersion")
	@ResponseBody
	public Object updateAppVersion(HttpServletRequest request,AppVersionDTO bean) throws Exception {		
		bean.setGmtModified(new Date());
		bean.setModifier(SessionManager.getCurrentUser().getId());
		logger.info("{}更新了app版本{}",SessionManager.getCurrentUser().getEmpName(),bean.getVersionNo());
		return appVersionService.updateObject(bean);
	}

	/**
	 * 
	 * <p>删除主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc 
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 * @throws Exception 
	 */
	@RequestMapping("/deleteAppVersion")
	@ResponseBody
	public Object deleteAppVersion(HttpServletRequest request,AppVersionDTO bean) throws Exception {
		logger.info("{}删除了app版本{}",SessionManager.getCurrentUser().getEmpName(),bean.getVersionNo());
		return appVersionService.deleteObject(bean);
	}
}
